<template>
  <!-- <div class=""></div> -->

  <!-- 组织结构中的节点的布局都是一样的，组件封装 -->
  <el-row type="flex" justify="space-between" align="middle" style="height: 40px;width: 100%">
    <el-col>
      <!-- 名称应该变成 对应的节点中的name -->
      <span>{{ treeNode.name }}</span>
    </el-col>
    <el-col :span="4">
      <el-row type="flex" justify="end">
        <!-- 两个内容 -->
        <el-col>{{ treeNode.manager }}</el-col>
        <el-col>
          <!-- 下拉菜单 element -->
          <el-dropdown @command="handleCommand">
            <span>
              操作<i class="el-icon-arrow-down" />
            </span>
            <!-- 下拉菜单 -->
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="add">添加子部门</el-dropdown-item>
              <!-- 根据是否为根节点来控制部门的删除 -->
              <el-dropdown-item v-if="!isRoot" command="edit">编辑部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" command="delete">删除部门</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { deleteDepartments } from '@/api/departments'
export default {
  name: 'TreeTools',

  props: {
    treeNode: {
      type: Object,
      required: true
    },

    isRoot: {
      type: Boolean,
      default: false
    }
  },

  data() {
    return {

    }
  },

  mounted() {

  },

  methods: {
    // 下拉菜单点击事件
    handleCommand(command) {
      // this.$message('click on item ' + command)

      if (command === 'add') {
        console.log('add')
        this.$emit('addDepts', this.treeNode)
      } else if (command === 'edit') {
        console.log('edit')

        this.$emit('editDepts', this.treeNode)
      } else if (command === 'delete') {
        this.$confirm('此操作将永久删除该部门, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async() => {
          const res = await deleteDepartments(this.treeNode.id)
          console.log(res)
          this.$emit('delDepts') // 触发自定义事件,通知父组件
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })

        // 删除部门
        console.log('delete')
      }
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
